package com.quanyue.haircut.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.quanyue.auto.fs.*;
import com.quanyue.auto.util.StringUtils;
import com.quanyue.auto.wechat.handler.MultiWxMpConfigStorage;
import com.quanyue.auto.wechat.handler.MultiWxPayConfig;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMassMessageService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpMassMessageServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public StoreService localStoreService(FsProperties fsProperties,
                                          FsConfigStorage fsConfigStorage,
                                          LocalDomainConfig domainConfig) {
        if (StringUtils.isBlank(fsProperties.getLocalPath())) {
            return new COSServiceImpl(fsConfigStorage);
        }
        return new LocalStoreServiceImpl(fsProperties, domainConfig);
    }

    @Bean
    public WxPayConfig wxPayConfig() {
        MultiWxPayConfig config = new MultiWxPayConfig();
        config.setTradeType("JSAPI");
        config.setNotifyUrl("/wechat/pay/confirm");
        return config;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService payService = new WxPayServiceImpl();
        payService.setConfig(payConfig);
        return payService;
    }

    @Bean
    public WxMpConfigStorage configStorage() {
        return new MultiWxMpConfigStorage();
    }

    @Bean
    public WxMpService wxMpService(WxMpConfigStorage configStorage) {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(configStorage);
        return wxMpService;
    }

    @Bean
    public WxMpMassMessageService wxMpMassMessage() {
        WxMpMassMessageService messageService = new WxMpMassMessageServiceImpl(this.wxMpService(this.configStorage()));
        return messageService;
    }
}
